/**
 * <p>
 * 版权所有：中企动力
 * </p>
 * <p>
 * 未经本公司许可，不得以任何方式复制或使用本程序任何部分
 * </p>
 */
package com.ce.pms.report.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ce.pms.report.dao.IReportDao;
import com.ce.pms.report.queryvo.ClientArrearageItemListQueryVO;
import com.ce.pms.report.vo.ClientArrearageItemListVo;
import com.ce.report.entity.CEReportDataSet;
import com.ce.report.exception.CEReportException;
import com.ce.report.service.ICEReportDataSetService;
import com.ce.report.vo.ReportsQueryBaseVo;
import com.ce.util.BlankUtil;
import com.ce.util.DateUtil;

/**
 * 客户欠款分项明细表
 *
 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
 * @Created 2008-11-14
 */

public class ClientArrearageItemListService  implements ICEReportDataSetService {

	private IReportDao reportDao;

	public CEReportDataSet getReportDataSet(ReportsQueryBaseVo arg0) throws CEReportException {

		ClientArrearageItemListQueryVO queryVO = (ClientArrearageItemListQueryVO) arg0;

		return new CEReportDataSet(getData(queryVO),getHeadInfo(queryVO));
	}

	/**
	 * 功能描述:获取列表数据  <br>
	 * @param queryVO
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2009-11-24  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private  List<ClientArrearageItemListVo> getData(ClientArrearageItemListQueryVO queryVO ){

		List<Object[]> temp = reportDao.getArrearageItemList(queryVO);
		List<ClientArrearageItemListVo> list = converToReportVoList(temp);
		return  list;

	}

	/**
	 * 功能描述: 转化到 VO  <br>
	 * @param dataList
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2009-11-25  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private List<ClientArrearageItemListVo> converToReportVoList(List<Object[]> dataList) {
		List<ClientArrearageItemListVo> list = new ArrayList<ClientArrearageItemListVo>();

		for (Object[] objTmp : dataList) {
			ClientArrearageItemListVo voTmp = new ClientArrearageItemListVo();

			voTmp.setHouseNumber((String) objTmp[0]);  			//房屋名称
			voTmp.setUserName((String)objTmp[1]);				//客户名称
			voTmp.setAcreage(setValue( objTmp[2]));				//面积
			voTmp.setAccounts((String) objTmp[3]);				//银行帐号
			voTmp.setCurArrearageCount(setValue(objTmp[4]));	//本月欠款合计
			voTmp.setCurrentCharge(setValue( objTmp[5]));		//本月费用
			voTmp.setLastMonthArrearage(setValue( objTmp[6]));//往月欠款
			voTmp.setMonthNumber(((Double)objTmp[7]).intValue() == 0 ? null:((Double)objTmp[7]).intValue() );//欠款月数
			voTmp.setLatefee(setValue( objTmp[8]));			//滞纳金
			voTmp.setOverheadExpenses(setValue( objTmp[9]));  //管理费
			voTmp.setOnticUpkeep(setValue(objTmp[10]));		//本体维修基金
			voTmp.setElectricityPay(setValue( objTmp[11]));	//电费
			voTmp.setWaterPay(setValue( objTmp[12]));			//水费
			voTmp.setSewagePay(setValue( objTmp[13]));		//污水处理费
			voTmp.setGarbagePay(setValue( objTmp[14]));		//垃圾处理费
			voTmp.setUpkeepPay(setValue( objTmp[15]));		//维修费

			list.add(voTmp);
		}
		return list;
	}


	//	传入的值必须是 BigDecimal 类型的,否则返回空如果为空或值为0 则返回空
	private BigDecimal setValue(Object obj){
		if (obj instanceof BigDecimal) {
			if(((BigDecimal)obj).intValue() == 0){
				return null;
			}
			return (BigDecimal)obj;
		}
		return null;
	}

	/**
	 * 功能描述:获取头信息 <br>
	 *
	 * @param queryVO
	 * @return Create author:黄彦军 <br>
	 *         Create on:2008-11-25 <br>
	 *         Edit author: <br>
	 *         Edit on: <br>
	 *         Why: <br>
	 */
	private Map<String, Object> getHeadInfo(
			ClientArrearageItemListQueryVO queryVO) {
		Map<String, Object> map = new HashMap<String, Object>();
		String computeMonth = DateUtil.convertDateToStr(queryVO
				.getComputeMonth(), "yyyy年MM月");
		map.put("reportHeadStr", "欠款月份：" + computeMonth);
		map.put("printDate", new Date());
		String subentry = "";
		if ("1".equals(queryVO.getSubentry())) {
			subentry = "本月费用";
		} else if ("2".equals(queryVO.getSubentry())) {
			subentry = "往月欠款";
		} else {
			subentry = "本月费用 + 往月欠款";
		}

		map.put("subentry", subentry);

		return map;
	}

	public void setReportDao(IReportDao reportDao) {
		this.reportDao = reportDao;
	}
}
